// ============================================================================
//
// Graph framework by Aurelien Moreau aurelien.moreau@gmail.com
// Modified by Pierre CHATEL chatelp@gmail.com
// Distributed under LGPL
// Copyright (C) Aurelien Moreau, 2008, 2009. All rights reserved
//
// ============================================================================

package com.thalesgroup.graphFramework.collections;

import java.util.HashMap;
import java.util.Map.Entry;

public class DETabular<K, T> {

	final protected HashMap<K, HashMap<K, T>> matrix;

	public DETabular() {
		matrix = new HashMap<K, HashMap<K, T>>();
	}

	public DETabular(final DETabular<K, T> source) {
		matrix = new HashMap<K, HashMap<K, T>>(source.matrix.size());
		for (final Entry<K, HashMap<K, T>> e : matrix.entrySet()) {
			final HashMap<K, T> nLine = new HashMap<K, T>(e.getValue());
			matrix.put(e.getKey(), nLine);
		}
	}

	public void clear() {
		matrix.clear();
	}

	public T get(final K k1, final K k2) {
		final HashMap<K, T> tmp = matrix.get(k1);
		return (tmp != null) ? tmp.get(k2) : null;
	}

	public T set(final K k1, final K k2, final T o) {
		HashMap<K, T> tmp = matrix.get(k1);
		if (tmp == null) {
			tmp = new HashMap<K, T>();
			matrix.put(k1, tmp);
		}
		return tmp.put(k2, o);
	}

}
